# -*- coding: utf-8 -*-
"""
Created on Tue May 17 16:24:42 2022

@author: angell
"""
from tkinter import *
from tkinter import messagebox

TYPE_DICT = {'int': 'Integer', 'varchar': 'String', 'char': 'String', 'nchar': 'String',\
             'nvarchar': 'String', 'text': 'String', 'ntext': 'String', 'tinyint': 'Integer',\
             'smallint': 'Integer', 'bit': 'Boolean', 'bigint': 'Long', 'float': 'Double',\
             'decimal': 'BigDecimal', 'money': 'BigDecimal', 'smallmoney': 'BigDecimal',\
             'numeric': 'BigDecimal', 'real': 'Float', 'uniqueidentifier': 'String',\
             'smalldatetime': 'Timestamp', 'datetime': 'Date', 'timestamp': 'byte[]', 'binary': 'byte[]',\
             'varbinary': 'byte[]', 'image': 'byte[]', 'sql_variant': 'String', 'double':'Double'}
    
def process(context):
    lines = context.strip().split("\n")
    res = ''
    for i in range(len(lines)):
        line = lines[i].strip().split(' ')
        while '' in line:
            line.remove('')
        field = line[0].strip('`')
        kind = TYPE_DICT[line[1].split('(')[0]]
    
        spl = line[0].strip('`').split('_')
        for j in range(len(spl)):
            if j == 0:
                variable = spl[0]
            else:
                variable += spl[j][0].upper() + spl[j][1:]
             
        comment = lines[i].strip().split('comment')[1].replace('\'','').replace(',','').strip()
        if i == 0:
            res += f'\n\t/**\n\t* {comment}\n\t*/\n\t@TableId(value = "{field}", type = IdType.ASSIGN_ID)\n\tprivate {kind} {variable};'
        elif variable == 'isDeleted':
            res += f'\n\t@TableLogic\n\t/**\n\t* {comment}\n\t*/\n\t@TableField("{field}")\n\tprivate {kind} {variable};'
        else:
            res += f'\n\t/**\n\t* {comment}\n\t*/\n\t@TableField("{field}")\n\tprivate {kind} {variable};'
    return res

root = Tk()
root.title("SqlToJava小工具")
root.resizable(0,0)
root.configure(bg="#bbb")
root.geometry('1100x610')                 

l1 = Label(root, text="Sql：", background="#bbb", font=('宋体', '12')).place(x=50, y=25)
input_text = Text(root, bg="#eee", font=('宋体', '12'))
input_text.pack() # 实例化文本框，否则对文本框内容get、delete、insert时会报'NoneType'
input_text.place(x=50, y=70, width=450, height=450)

l2 = Label(root, text="Result：", background="#bbb", font=('宋体', '12')).place(x=590, y=20)
result_text = Text(root, bg="#eee", font=('宋体', '12'))
result_text.pack()
result_text.place(x=590, y=65, width=450, height=500)

def on_click_clean():
    input_text.delete(1.0,END)
    
def on_click_output():
    context = input_text.get('0.1','end')
    try:
        result = process(context)
        result_text.delete(1.0,END)
        result_text.insert(END,result)
    except:
        string ='未发现数据！请重新输入！              '
        messagebox.showinfo(title='提示', message = string)
        
button_clean = Button(root, text="Clean", bg="#7AD4F1", font=('Arail', '10'), command = on_click_clean)
button_clean.place(x=305, y=550, width=60, height=25)
button_change = Button(root, text="Change", bg="yellow", font=('Arail', '10'), command = on_click_output)
button_change.place(x=380, y=550, width=100, height=25)

root.mainloop()

